package net.cloudsun.graph.puzzle.relation;

import net.cloudsun.graph.puzzle.JigsawState;
import net.cloudsun.graph.puzzle.Pair;
import net.cloudsun.graph.puzzle.PuzzleDirection;
import net.cloudsun.graph.puzzle.PuzzleStateRelation;

import java.util.function.Function;

/**
 * 逆时针旋转
 * created at 25/11/2021
 *
 * @author 俞建波
 * <a href="mailto://yujianbo@chtwm.com">yujianbo@chtwm.com</a>
 * @since 1.2.0
 */
public enum AnticlockwiseRotate implements Function<JigsawState,JigsawState> {
    INSTANCE;

    /**
     * 总共8个位置
     * 每个位置只有一个方向。这就是顺时针旋转
     * 右 右 下
     * 上 空 下
     * 上 左 左
     * 逆运算
     * 下 左 左
     * 下 空 上
     * 右 右 上
     * @param start
     * @return
     */
    @Override
    public JigsawState apply(JigsawState start) {
        // 先获取位置
        final Pair<Integer> location = PuzzleStateRelation.getLocation(start, 8);
        // 如果
        final Integer row = location.getA();
        final Integer column = location.getB();
        final int[][] ints = start.toArray();
        PuzzleDirection direction = null;
        if (row == 0 && column != 0) {
            direction = PuzzleDirection.LEFT;
        } else if (column == 2 && row != 0) {
            direction = PuzzleDirection.UP;
        } else if (row == 2 && column != 2) {
            direction = PuzzleDirection.RIGHT;
        } else if (column == 0 && row != 2) {
            direction = PuzzleDirection.DOWN;
        }

        PuzzleStateRelation.swap(ints, location, direction);
        return new JigsawState(ints);
    }
}
